package org.blogwu.learn.netty学习.chapter0.bio;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Server2 {

    static ExecutorService executorService = Executors.newFixedThreadPool(10);


    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("开始监听");

        while (true) {
            Socket socket = serverSocket.accept();
            executorService.submit(() -> {
                        try (InputStream inputStream = socket.getInputStream();
                             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                             OutputStream outputStream = socket.getOutputStream();
                             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
                        ) {
                            System.out.println("连接的ip：");
                            String str = null;
                            while ((str = bufferedReader.readLine()) != null) {
                                if (str.length() <= 0) {
                                    System.out.println("ccc");
                                    break;
                                }
                                System.out.println("客户端传入数据：" + str);
                            }
                            bufferedWriter.write("HTTP/1.1 200 OK");
                            bufferedWriter.write("Content-Length: 11");
                            bufferedWriter.write("Hello World");
                            bufferedWriter.flush();
                            outputStream.flush();
                            System.out.println("服务端传输完毕！");
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {

                        }

                    }
            );
        }


    }
}
